package com.healthcloud.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.healthcloud.dao.TestProjectItemMapper;
import com.healthcloud.dao.TestProjectKindMapper;
import com.healthcloud.dao.TestProjectMapper;
import com.healthcloud.dao.TestReferenceValueMapper;
import com.healthcloud.entity.TestProject;
import com.healthcloud.entity.TestProjectItem;
import com.healthcloud.entity.TestProjectKind;
import com.healthcloud.entity.TestReferenceValue;
import com.healthcloud.service.TestProjectService;
import com.healthcloud.utils.PageParam;

/**
 * Created by houxianghua on 2015/7/22.
 */
@Service
@Transactional
public class TestProjectServiceImpl implements TestProjectService {

    @Autowired
    private TestProjectMapper testProjectMapper;

    @Autowired
    private TestProjectKindMapper testProjectKindMapper;

    @Autowired
    private TestProjectItemMapper testProjectItemMapper;

    @Autowired
    private TestReferenceValueMapper testReferenceValueMapper;


    @Override
    public TestProject get(int id) {
        return testProjectMapper.get(id);
    }

    @Override
    public TestProjectKind getKindByCode(String testKindCode) {
        return testProjectKindMapper.get(testKindCode);
    }

    @Override
    public TestProjectItem getItemByCode(String testItemCode) {
        return testProjectItemMapper.get(testItemCode);
    }

    @Override
    public TestReferenceValue getReferenceById(int id) {
        return testReferenceValueMapper.get(id);
    }

    @Override
    public List<TestReferenceValue> getReferenceByItemCode(String testItemCode) {
        return testReferenceValueMapper.getByItemCode(testItemCode);
    }

    @Override
    public List<TestProject> findByEntityAll(TestProject entity) {
        return testProjectMapper.findByEntityAll(entity);
    }

    @Override
    public List<TestProjectKind> findByEntityAll(TestProjectKind entity) {
        return testProjectKindMapper.findByEntityAll(entity);
    }

    @Override
    public List<TestProjectItem> findByEntityAll(TestProjectItem entity) {
        return testProjectItemMapper.findByEntityAll(entity);
    }

    @Override
    public List<TestReferenceValue> findByEntityAll(TestReferenceValue entity) {
        return testReferenceValueMapper.findByEntityAll(entity);
    }

    @Override
    public List<TestProject> search(TestProject entity, PageParam pageParam) {
        return testProjectMapper.search(entity, pageParam);
    }

    @Override
    public int count(TestProject entity) {
        return testProjectMapper.count(entity);
    }

    @Override
    public int save(TestReferenceValue entity) {
        return testReferenceValueMapper.save(entity);
    }

    @Override
    public int update(TestReferenceValue entity) {
        return testReferenceValueMapper.update(entity);
    }

    @Override
    public int deletes(int[] ids) {
        for (int i=0; i<ids.length; i++){
            testReferenceValueMapper.delete(ids[i]);
        }
        return 0;
    }

    /**
     * ajax验证年龄（男）的值是否已经存在
     *
     * @param minAgeTest 年龄最小值（男）
     * @param maxAgeTest 年龄最大值（男）
     * @param testItemCode 项目id
     * @param exception 参考值主键id（修改时使用）
     * @reurn true:验证通过
     * */
    @Override
    public boolean validateAgeRange(Integer minAgeTest, Integer maxAgeTest, String testItemCode, String exception) {
        if (minAgeTest > maxAgeTest) {
            return false;
        }
        List<TestReferenceValue> trvList = getReferenceByItemCode(testItemCode);
        //删除要修改的那条数据
        if(StringUtils.isNotBlank(exception)){
            for (int i = 0; i <trvList.size() ; i++) {
                System.out.println(trvList.get(i).getId());
                System.out.println(exception);
                if((String.valueOf(trvList.get(i).getId())).equals(exception)){
                    trvList.remove(i);
                }
            }
        }
        //判断是否与现有数据有交集
        boolean b = true;
        for (int i = 0; i <trvList.size() ; i++) {
            Integer minAge = trvList.get(i).getMinAge();
            Integer maxAge = trvList.get(i).getMaxAge();
            if(minAgeTest>maxAge){
                continue;
            }
            if(maxAgeTest<minAge){
                continue;
            }
            b = false;
            break;
        }
        return b;
    }

    /**
     * ajax验证年龄（女）的值是否已经存在
     *
     * @param minAgeGirlTest 年龄最小值（女）
     * @param maxAgeGirlTest 年龄最大值（女）
     * @param testItemCode 项目id
     * @param exception 参考值主键id（修改时使用）
     * @reurn true:验证通过
     * */
    @Override
    public boolean validateAgeGirlRange(Integer minAgeGirlTest, Integer maxAgeGirlTest, String testItemCode, String exception) {
        if (minAgeGirlTest > maxAgeGirlTest) {
            return false;
        }
        List<TestReferenceValue> trvList = getReferenceByItemCode(testItemCode);
        //删除要修改的那条数据
        if(StringUtils.isNotBlank(exception)){
            for (int i = 0; i <trvList.size() ; i++) {
                if(trvList.get(i).getId().equals(exception)){
                    trvList.remove(i);
                }
            }
        }
        //判断是否与现有数据有交集
        boolean b = true;
        for (int i = 0; i <trvList.size() ; i++) {
            Integer minAgeGirl = trvList.get(i).getMinAgeGirl();
            Integer maxAgeGirl = trvList.get(i).getMaxAgeGirl();
            if(minAgeGirlTest>maxAgeGirl){
                continue;
            }
            if(maxAgeGirlTest<minAgeGirl){
                continue;
            }
            b = false;
            break;
        }
        return b;
    }

	@Override
	public List<TestProjectItem> findTestProjectItem() {

		return testProjectItemMapper.findTestProjectItem();
	}
    
    
}
